package pres.wchen.oa.cor.shiro.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pres.wchen.oa.cor.shiro.model.BaseModel;
import pres.wchen.oa.cor.shiro.model.SysRole;
import pres.wchen.oa.cor.shiro.model.SysUser;
import pres.wchen.oa.cor.shiro.service.SysRoleService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 源文件名：SysRoleController.java
 * 作者：wchen
 * 创建时间：2018/1/14
 * 文件描述：角色表
 * 版权所有：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息： 
 */
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController {
	@Autowired
	private SysRoleService sysRoleService;


	/**
	 * @方法描述 主页
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/index")
	public ModelAndView index(HttpSession session, Integer pageNo, Integer pageSize) {
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		BaseModel baseModel = sysRoleService.list(conditionMap);
		return super.dealListResult("/role/index", baseModel);
	}


	/**
	 * @方法描述 新增
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@PostMapping(value = "/")
	public Map insert(SysRole sysRole) {
		Long id = sysRoleService.insert(sysRole);
		return super.dealInsertResult(id);
	}


	/**
	 * @方法描述 删除
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@DeleteMapping(value = "/{id}")
	public Map delete(@PathVariable("id") Long id) {
		Integer result = sysRoleService.delete(id);
		return super.dealDeleteResult(result);
	}


	/**
	 * @方法描述 更新
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@PutMapping(value = "/")
	public Map update(SysRole sysRole) {
		Integer result = sysRoleService.update(sysRole);
		return super.dealUpdateResult(result);
	}


	/**
	 * @方法描述 详情
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/{id}")
	public Map info(@PathVariable("id") Long id) {
		SysRole sysRole = sysRoleService.info(id);
		return super.dealInfoResult(sysRole);
	}


	/**
	 * @方法描述 分页列表
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/pageList")
	public Map pageList(HttpServletRequest request) {
		Object pageSize = request.getParameter("pageSize");
		Object pageNo = request.getParameter("pageNo");
		Object roleName = request.getParameter("roleName");
		Object pageFlag = request.getParameter("pageFlag");
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		conditionMap.put("roleName", roleName);
		conditionMap.put("pageFlag", pageFlag);
		BaseModel baseModel = sysRoleService.list(conditionMap);
		return super.dealListResult( baseModel);
	}


	/**
	 * @方法描述 查询用户角色列表(区分是否勾选)
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/allotRoles/list")
	public Map chooseUserRolesList(HttpServletRequest request) {
		Object userId = request.getParameter("userId");
		Object pageFlag = request.getParameter("pageFlag");
		Map conditionMap = new HashMap();
		conditionMap.put("userId", userId);
		conditionMap.put("pageFlag", pageFlag);
		BaseModel baseModel = sysRoleService.chooseUserRoleList(conditionMap);
		return super.dealListResult( baseModel);
	}




	/**
	 * @方法描述 查询列表
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@PostMapping(value = "/list")
	public Map pageList(String roleName) {
		Map conditionMap = new HashMap();
		conditionMap.put("roleName", roleName);
		BaseModel baseModel = sysRoleService.list(conditionMap);
		return super.dealListResult(baseModel);
	}


	/**
	 * @方法描述 权限分配角色分页列表
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/20
	 */
	@GetMapping(value = "/allotPermissions/pageList")
	public ModelAndView rolePageList(HttpServletRequest request) {
		Object pageNo = request.getParameter("pageNo");
		Object pageSize = request.getParameter("pageSize");
		Object roleName = request.getParameter("roleName");
		Object pageFlag = request.getParameter("pageFlag");
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		conditionMap.put("roleName", roleName);
		conditionMap.put("pageFlag", pageFlag);
		BaseModel baseModel = sysRoleService.list(conditionMap);
		return super.dealListResult("/role/allot_permission_table", baseModel);
	}


	/**
	 * @方法描述 角色权限分配主页
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/allotPermissions/index")
	public ModelAndView rolePermissionIndex(HttpSession session, Integer pageNo, Integer pageSize) {
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		BaseModel baseModel = sysRoleService.list(conditionMap);
		return super.dealListResult("/role/allot_permission", baseModel);
	}


	/**
	 * @方法描述 角色分配权限资源
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@PostMapping(value = "/allotPermissions/addPermission")
	public Map allotRolePermissions(@RequestBody Map map) {
		Long roleId = Long.valueOf(map.get("roleId").toString());
		List permissionIdList = (List) map.get("permissionIdList");
		//清除原有权限
		sysRoleService.deleteRolePermissions(roleId);

		//添加赋予的权限
		if(permissionIdList.size()> 0) {
			sysRoleService.addRolePermissions(map);
		}
		return super.dealInfoResult(null);
	}

}